package api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import donain.Order;
import donain.OrderItem;
import donain.Product;
import donain.User;
import service.ProductService;
import utilts.GetRequestJsonUtils;
import utilts.Response;
import utilts.ResponseCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CommitOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject object= null;
        resp.setContentType("application/json;charset=utf-8");
        try {
            object = GetRequestJsonUtils.getRequestJsonObject(req);
            JSONArray cart = object.getJSONArray("cart");
            JSONObject receiver = object.getJSONObject("receiver");
            ProductService productService = new ProductService();
            HttpServletRequest httpServletRequest=(HttpServletRequest) req;
            Order order=new Order();
            User user=new User();
            order.setId(UUID.randomUUID().toString());
            double total=0;
            List<OrderItem> items=new ArrayList<>();
            for (int i = 0; i < cart.size(); i++) {
                JSONObject book = (JSONObject) cart.get(i);
                OrderItem item=new OrderItem();
                item.setOrder(order);
                int number = book.getIntValue("number");
                String name=book.getString("name");
                String id = book.getString("id");
                double lineTotal=book.getDoubleValue("lineTotal");
                total+=lineTotal;
                Product product = productService.findProductById(id);
                item.setP(product);
                item.setBuynum(number);
                if (product.getPnum() < number) {
                    Response   build = Response.build(ResponseCode.ERROR_NUMBER);

                    resp.getWriter().write(JSON.toJSON(build).toString());
                    resp.setContentType("application/json;charset=utf-8");
                    return;
                }
            }
            String receiverName = null, receiverphone = null, receiveraddress = null;
            receiverName = receiver.getString("name");
            receiverphone = receiver.getString("phone");
            receiveraddress = receiver.getString("address");

            order.setMoney(total);
            order.setReceiverName(receiverName);
            order.setReceiverPhone(receiverphone);
            order.setReceiverAddress(receiveraddress);
            order.setPaystate(0);
            order.setUser(user);
            order.setOrderItems(items);
            Response build = Response.build(ResponseCode.SUCCESS,total);
            resp.getWriter().write(JSON.toJSON(build).toString());
            resp.setContentType("application/json;charset=utf-8");
        }catch (Exception e) {
            e.printStackTrace();
            Response build = Response.build(ResponseCode.ERROR_NUMBER);
            resp.getWriter().write(JSON.toJSON(build).toString());
            resp.setContentType("application/json;charset=utf-8");
        }
    }
}
